//
//  SYDWalletDetailViewController.m
//  PinGJByOwnerSide
//
//  Created by lantaiyuan on 2017/8/8.
//  Copyright © 2017年 lantaiyuan. All rights reserved.
//

#import "SYDWalletDetailViewController.h"
#import "SYDWalletDetailCell.h"

#import "SYDMyWalletDetailModel.h"
static NSString *CellTableIdentifier = @"SYDWalletDetailCell";
static NSString *CellTableIdentifier2 = @"SYDWalletDetailCell2";
static NSString *CellTableIdentifier3 = @"SYDWalletDetailCell3";
@interface SYDWalletDetailViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tableView;

@property(nonatomic,strong)NSMutableArray *cashingGroups;
@property(nonatomic,strong)NSMutableArray *vehicleFees;
@property(nonatomic,strong)NSMutableArray *cashingRefundingGroups;

@property (nonatomic, strong) YMNodataView * nodataView;
@property (nonatomic, strong) YMNodataView * noNetView;
@property (nonatomic, strong) YMNodataView * noServerView;
@property(nonatomic,strong)CashingOrderModel *model;
@end

@implementation SYDWalletDetailViewController
#pragma mark - 导航栏
- (void)navigationBarHandle{
    
    // 导航栏返回按钮
    WEAKSELF
    [self backButtonAction:^{
        
        [weakSelf.navigationController popViewControllerAnimated:YES];
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationBar.title = @"钱包明细";
    [self navigationBarHandle];
    [self initWalletDetailUI];
}

-(void)initWalletDetailUI{
    //偏移20/64适配
    if (@available(iOS 11.0,*)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;//UIScrollView也适用
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    self.tableView.separatorColor = SEPARATE_LINE_COLOR;
    [self.tableView registerNib:[UINib nibWithNibName:CellTableIdentifier bundle:nil] forCellReuseIdentifier:CellTableIdentifier];
    [self.tableView registerNib:[UINib nibWithNibName:CellTableIdentifier bundle:nil] forCellReuseIdentifier:CellTableIdentifier2];
    [self.tableView registerNib:[UINib nibWithNibName:CellTableIdentifier bundle:nil] forCellReuseIdentifier:CellTableIdentifier3];
    [self getWalletDetailFromServer];
}
-(void)getWalletDetailFromServer{
    WEAKSELF  //
    [[SYDNetwork shareTool]getMineWalletDetailWithuserId:[YMSaveTool valueForKey:USER_ID] success:^(NSURLSessionDataTask *task, id responseObject) {
        @try {
            //NSLog(@"------responseObject:%@",[responseObject mj_JSONString]);
            if ([responseObject[@"statusCode"]integerValue] == 200) {
                SYDMyWalletDetailModel *myWalletDetailModel = [SYDMyWalletDetailModel mj_objectWithKeyValues:responseObject[@"obj"]];
            
                [self.cashingGroups removeAllObjects];
                [self.cashingRefundingGroups removeAllObjects];
                [self.vehicleFees removeAllObjects];
                
                if (myWalletDetailModel.cashingGroups.count > 0) {
                    weakSelf.cashingGroups = [CashingGroupModel mj_objectArrayWithKeyValuesArray:myWalletDetailModel.cashingGroups];
        
                }
                if (myWalletDetailModel.vehicleFees.count > 0) {
                
                    weakSelf.vehicleFees = [VehicleFeesModel mj_objectArrayWithKeyValuesArray:myWalletDetailModel.vehicleFees];
                }
                if (myWalletDetailModel.cashingRefundingGroups.count > 0) {
                
                    weakSelf.cashingRefundingGroups = [CashingGroupModel mj_objectArrayWithKeyValuesArray:myWalletDetailModel.cashingRefundingGroups];
                }
            }else{
                [SVProgressHUD displayInfoWithStatus:responseObject[@"msg"]];
            }
        } @catch (NSException *exception) {
            NSLog(@"exception:L%@",exception.description);
        } @finally {
        }
        if (weakSelf.cashingGroups.count > 0 ||weakSelf.cashingRefundingGroups.count > 0 || weakSelf.vehicleFees.count > 0) {
            
        } else {
            weakSelf.tableView.tableFooterView = weakSelf.nodataView;
        }
        [weakSelf.tableView reloadData];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error.description);
    }];
}

#pragma mark---UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 3;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section == 0) {
        return  self.cashingGroups.count;
    }else if (section == 1){
        return self.cashingRefundingGroups.count;
    }else if (section == 2){
        return self.vehicleFees.count;
    }
    return 0;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        SYDWalletDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:CellTableIdentifier forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (!cell) {
            cell = [[SYDWalletDetailCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellTableIdentifier];
        }
        CashingGroupModel *model = self.cashingGroups[indexPath.row];
        [cell walletDetailWithCashingGroupModel:model vehicleFeesModel:nil status:1];
        return  cell;
    }else if (indexPath.section == 1){
        SYDWalletDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:CellTableIdentifier2 forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (!cell) {
            cell = [[SYDWalletDetailCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellTableIdentifier2];
        }
        CashingGroupModel *model = self.cashingRefundingGroups[indexPath.row];
        [cell walletDetailWithCashingGroupModel:model vehicleFeesModel:nil status:2];
        return  cell;
    }else if (indexPath.section == 2){
        SYDWalletDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:CellTableIdentifier3 forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (!cell) {
            cell = [[SYDWalletDetailCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellTableIdentifier3];
        }
         VehicleFeesModel *model = self.vehicleFees[indexPath.row];
        [cell walletDetailWithCashingGroupModel:nil vehicleFeesModel:model status:3];
        return  cell;
    }
    
    return nil;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 65;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    SYDReturnScheduleViewController *scheduleVc = [[SYDReturnScheduleViewController alloc]init];
    
    if (indexPath.section == 0) {
        CashingGroupModel *model = self.cashingGroups[indexPath.row];
        scheduleVc.cashingGroupModel = model;
        scheduleVc.status = 1;
    }else if (indexPath.section == 1){//提现退款对象数组
        CashingGroupModel *model = self.cashingRefundingGroups[indexPath.row];
        scheduleVc.status = 2;
        scheduleVc.cashingGroupModel = model;
    }else if (indexPath.section == 2){
        VehicleFeesModel *model = self.vehicleFees[indexPath.row];
        scheduleVc.status = 3;
        scheduleVc.vehicleFeesModel = model;
    }
    [self.navigationController pushViewController:scheduleVc animated:YES];
}

-(void)tableView:(UITableView *)tableView willDisplayHeaderView:(nonnull UIView *)view forSection:(NSInteger)section{
    
    view.tintColor = [UIColor clearColor];
}
#pragma mark - UITableViewDelegate

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    if (section == 0) {
        if (self.cashingGroups.count > 0 ||self.cashingRefundingGroups.count > 0 || self.vehicleFees.count > 0) {
            return [self sectionHeader];
        } else {
            return nil;
        }
    }
    return nil;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    if (section == 0) {
        if (self.cashingGroups.count > 0 ||self.cashingRefundingGroups.count > 0 || self.vehicleFees.count > 0) {
            return 39;
        }
        return 0.00001;
    }
    return 0.00001;
}


- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.00001;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] init];
}
-(UIView *)sectionHeader{
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 39)];
    view.backgroundColor = CONTENT_BG_COLOR;
    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, SCREEN_WIDTH - 15, view.height)];
    label.text = @"本月";
    label.textColor = SUB_TITLE_COLOR;
    label.font = [UIFont systemFontOfSize:13];
    [view addSubview:label];
    return view;
}
#pragma mark - Lazy
-(UITableView *)tableView{
    
    if (_tableView == nil) {
        if (iPhone8_X) {
            _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 88, SCREEN_WIDTH, SCREEN_HEIGHT - 88) style:UITableViewStyleGrouped];
        } else {
            _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, STATUS_AND_NAVIGATION_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT - STATUS_AND_NAVIGATION_HEIGHT) style:UITableViewStyleGrouped];
        }
        _tableView.backgroundColor = CONTENT_BG_COLOR;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tipsStr = @"暂无钱包记录！";
        _tableView.tipsImg = [UIImage imageNamed:@"icon_wallet _record"];
        [self.view addSubview:_tableView];
    }
    return _tableView;
}
#pragma mark---setter
-(CashingOrderModel *)model{
    if (!_model) {
        _model = [[CashingOrderModel alloc]init];
    }
    return _model;
}
//无数据
- (YMNodataView *)nodataView{
    if (!_nodataView) {
        if (iPhone8_X) {
            _nodataView = [[YMNodataView alloc]initWithFrame:CGRectMake(0, 88, SCREEN_WIDTH, SCREEN_HEIGHT - 88) image:[UIImage imageNamed:@"icon_withdrawal"] imageWidth:65 title:@"暂无钱包记录!"];
        } else {
            _nodataView = [[YMNodataView alloc]initWithFrame:CGRectMake(0, STATUS_AND_NAVIGATION_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT - STATUS_AND_NAVIGATION_HEIGHT) image:[UIImage imageNamed:@"icon_withdrawal"] imageWidth:65 title:@"暂无钱包记录!"];
        }
        
    }
    return _nodataView;
}

- (void)showNodata{
    self.tableView.tableFooterView = self.nodataView;
}

- (void)hideNodata{
    self.tableView.tableFooterView = nil;
    
}

//无网络,连接失败
- (YMNodataView *)noNetView{
    if (!_noNetView) {
        if (iPhone8_X) {
            _nodataView = [[YMNodataView alloc]initWithFrame:CGRectMake(0, 88, SCREEN_WIDTH, SCREEN_HEIGHT - 88) image:[UIImage imageNamed:@"icon_withdrawal"] imageWidth:65 title:@"网络连接失败,请检查您的网络!"];
        } else {
            _nodataView = [[YMNodataView alloc]initWithFrame:CGRectMake(0, STATUS_AND_NAVIGATION_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT - STATUS_AND_NAVIGATION_HEIGHT) image:[UIImage imageNamed:@"icon_withdrawal"] imageWidth:65 title:@"网络连接失败,请检查您的网络!"];
        }
        
    }
    return _noNetView;
}

- (void)showNoNetView{
    self.tableView.tableFooterView = self.noNetView;
}

- (void)hideNoNetView{
    self.tableView.tableFooterView = nil;
}

// 服务器错误
- (YMNodataView *)noServerView{
    if (!_noServerView) {
        if (iPhone8_X) {
            _nodataView = [[YMNodataView alloc]initWithFrame:CGRectMake(0, 88, SCREEN_WIDTH, SCREEN_HEIGHT - 88) image:[UIImage imageNamed:@"icon_withdrawal"] imageWidth:65 title:@"服务器连接错误,请稍后再试!"];
        } else {
            _nodataView = [[YMNodataView alloc]initWithFrame:CGRectMake(0, STATUS_AND_NAVIGATION_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT - STATUS_AND_NAVIGATION_HEIGHT) image:[UIImage imageNamed:@"icon_withdrawal"] imageWidth:65 title:@"服务器连接错误,请稍后再试!"];
        }
    }
    return _noServerView;
}

- (void)showNoServerView{
    self.tableView.tableFooterView = self.noServerView;
}
-(NSMutableArray *)vehicleFees{
    if (!_vehicleFees) {
        _vehicleFees = [NSMutableArray array];
    }
    return _vehicleFees;
}
-(NSMutableArray *)cashingGroups{
    if (!_cashingGroups) {
        _cashingGroups = [NSMutableArray array];
    }
    return _cashingGroups;
}
-(NSMutableArray *)cashingRefundingGroups{
    if (!_cashingRefundingGroups) {
        _cashingRefundingGroups = [NSMutableArray array];
    }
    return  _cashingRefundingGroups;
}
- (void)hideNoServerView{
    self.tableView.tableFooterView = nil;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end

//-(void)getCashingProgressFromServer{
//    [[SYDNetwork shareTool]getMineCashingDetailAdditionWithuserId:[YMSaveTool valueForKey:USER_ID] success:^(NSURLSessionDataTask *task, id responseObject) {
//        @try {
//            
//            if ([responseObject[@"obj"]isKindOfClass:[NSString class]]) {
//                
//            }else if([responseObject[@"obj"]isKindOfClass:[NSDictionary class]]){
//                NSLog(@"------responseObject:%@",responseObject);
//                NSDictionary *jsondict = responseObject[@"obj"];
//                CashingOrderModel *cashModel = [CashingOrderModel mj_objectWithKeyValues:jsondict];
//                self.model = cashModel;
//            }
//        } @catch (NSException *exception) {
//            NSLog(@"exception-----:%@",exception.description);
//        } @finally {
//        }
//    } failure:^(NSURLSessionDataTask *task, NSError *error) {
//        NSLog(@"error:%@",error.description);
//    }];
//}







































